
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for sent_message.
	''' </summary>
	Public Partial Class messages_sent
		Inherits BaseWebPage
		Protected chkPostedbul As System.Web.UI.WebControls.CheckBox
		Protected chkPostedbul1 As System.Web.UI.WebControls.CheckBox
		Protected lblSelectall_bulletin As System.Web.UI.WebControls.Label
		Protected lblClearall_bulletin As System.Web.UI.WebControls.Label
		Protected litTrash As System.Web.UI.WebControls.Literal

		Public startIndex As Integer
		Private strDefaultOrderBy As String = "Date"


		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.SENT_MESSAGE_PAGE;

			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.MESSAGES_SENT_PAGE, "", Form1, Nothing, Nothing)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucTabs.ActiveTab = 2
			ucTabs.PageName = Constants.SENT_MESSAGE

			Dim msgs As ArrayList = Messages.GetUserOutboxMessages(usr.ID, False)
			Dim MessagesCount As Double = msgs.Count
			If MessagesCount = 0 Then
				tblMessages.Visible = False
				lblNoMessages.Visible = True
			Else
				dgPager.RedirectPage = Constants.SENT_MESSAGE
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(MessagesCount / Constants.MAX_MESSAGES_ON_PAGE))
				dgPager.Action = "-1"
				dgPagerUp.RedirectPage = Constants.SENT_MESSAGE
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(MessagesCount / Constants.MAX_MESSAGES_ON_PAGE))
				dgPagerUp.Action = "-1"

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(MessagesCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(MessagesCount)
				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_MESSAGES_ON_PAGE
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)

				If Not IsPostBack Then
					dgMessagesSent.VirtualItemCount = Convert.ToInt32(MessagesCount)
					BindGrid(strDefaultOrderBy)
				End If
			End If
		End Sub
		Protected Function CreateDataSource() As ICollection
			Dim usr As BO.User = LoggedUser

			Dim dt As New DataTable()
			Dim msgList As ArrayList
			Dim curMessageDate As DateTime

			' List user's inbox messages
			msgList = Messages.GetUserOutboxMessages(LoggedUser.ID, False)
			'bulletinList = Messages.GetUserInboxBulletins( LoggedUser.ID, false );
			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			Dim MessagesCount As Integer = msgList.Count
			If MessagesCount <= 0 Then
				dgPager.FirstPageNumber = 1
				dgPager.LastPageNumber = 1
				dgPager.MaxPageNumber = 1
				dgPager.PagesRest = 1
				dgPager.dgListing.StartIndex = 0
				dgPager.dgListing.EndIndex = 0
				dgPagerUp.FirstPageNumber = 1
				dgPagerUp.LastPageNumber = 1
				dgPagerUp.MaxPageNumber = 1
				dgPagerUp.PagesRest = 1
				dgPagerUp.dgListing.StartIndex = 0
				dgPagerUp.dgListing.EndIndex = 0
				dgMessagesSent.AllowSorting = False
			Else
				dgMessagesSent.AllowSorting = True
				Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
				Dim endIndex As Integer = startIndex + Constants.MAX_MESSAGES_ON_PAGE
				Dim t As Integer = 0
				Dim counter As Integer = 0

				endIndex = Math.Min(endIndex, MessagesCount)
				t = (endIndex - startIndex)
				dgPager.dgListing.StartIndex = startIndex + 1
				dgPager.dgListing.EndIndex = endIndex
				dgPagerUp.dgListing.StartIndex = startIndex + 1
				dgPagerUp.dgListing.EndIndex = endIndex

				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("MessageID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("Date", GetType(String)))
				dt.Columns.Add(New DataColumn("To", GetType(String)))
				dt.Columns.Add(New DataColumn("TypeImageURL", GetType(String)))
				dt.Columns.Add(New DataColumn("Subject", GetType(String)))
				dt.Columns.Add(New DataColumn("ViewMsgURL", GetType(String)))
				dt.Columns.Add(New DataColumn("TypeName", GetType(String)))

				Dim recipientsList As ArrayList
				Dim rcptsLinks As String = ""
				Dim curRecipientID As Integer
				Dim curRecipient As User
				Dim pic As UserPicture

				' List user's inbox messages
				For Each msg As Message In msgList
					If counter >= startIndex AndAlso counter < endIndex Then
						dr = dt.NewRow()
						curMessageDate = msg.DateSent

						dr(0) = msg.ID
						dr(1) = Constants.ARR_MONTHS(curMessageDate.Month - 1) + " " + curMessageDate.Day + "<br />" + curMessageDate.ToShortTimeString()
						recipientsList = msg.Recipients
						rcptsLinks = ""
						Dim j As Integer = 0
						While j < recipientsList.Count
							curRecipientID = DirectCast(recipientsList(j), Integer)
							curRecipient = Users.GetUserByID(curRecipientID)

							pic = curRecipient.DefaultPicture

							rcptsLinks += "<a href='" + Constants.MEMBERS + "?id=" + curRecipientID + "'>" + getBeginning(HttpUtility.HtmlEncode(curRecipient.Name), Constants.NAME_MAX_VISIBLE_CHARS) + "</a>"

							If pic <> Nothing Then
								rcptsLinks += "<table cellspacing='0' cellpadding='0' border='0'>" + "<tr><td class='data_grid_clear'>" + "<a href='" + Constants.MEMBERS + "?id=" + curRecipientID + "'>" + "<img src='" + Pictures.GetPictureSrcFullURL(pic, True, "") + "' class='small_image'></a>" + "</td></tr>"
							Else
								rcptsLinks += "<table cellspacing='0' cellpadding='0' border='0'>" + "<tr><td class='data_grid_clear'>" + "<a href='" + Constants.MEMBERS + "?id=" + curRecipientID + "'>" + "<img src='" + Constants.DEFAULT_THUMBNAIL_IMAGE_URL.Substring(Constants.DEFAULT_THUMBNAIL_IMAGE_URL.IndexOf("/") + 1) + "' class='small_image'></a>" + "</td></tr>"
							End If

							If LoggedUsersList.Contains(curRecipientID) Then
								rcptsLinks += "<tr><td align='center' class='data_grid_clear'>" + "<img src='images/online_status.gif' >" + "</td></tr>"
							End If

							If j < recipientsList.Count - 1 Then
								rcptsLinks += "<br>&nbsp;<br>"
							End If
							rcptsLinks += "</table>"
							System.Math.Max(System.Threading.Interlocked.Increment(j),j - 1)
						End While
						dr(2) = rcptsLinks
						dr(4) = Server.HtmlEncode(msg.Subject)
						Select Case msg.MessageType
							Case Messages.MessageType.mtBulletin
								dr(3) = Constants.BULLETIN_IMAGE_URL_BIG
								dr(6) = Constants.MSG_TYPE_NAME_BULLETIN
								Exit Select
							Case Messages.MessageType.mtGroupBulletin
								dr(3) = Constants.GROUP_BULLETIN_IMAGE_URL_BIG
								dr(6) = Constants.MSG_TYPE_NAME_GRP_BULLETIN
								Exit Select
							Case Else
								dr(3) = Constants.MESSAGE_IMAGE_URL_BIG
								dr(6) = Constants.MSG_TYPE_NAME_MESSAGE
								Exit Select
						End Select
						dr(5) = "../" + Constants.VIEW_MESSAGE + "?id=" + msg.ID

						dt.Rows.Add(dr)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid(ByVal orderBy As String)
			'init datagrid with database data ordered by "orederBy" param
			dgMessagesSent.DataSource = CreateDataSource()
			dgMessagesSent.DataBind()
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
		End Sub
#End Region

		Private Sub dgMessagesSent_Sort(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)
			BindGrid(e.SortExpression)
		End Sub

		Private Sub dgMessagesSent_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim cmd As String = e.CommandName

			If cmd.Equals("MemberRedirect") Then
				Response.Redirect(Constants.MEMBERS + "?id=" + e.CommandArgument)
			End If
		End Sub
		Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim cbCheckedValue As String
			Dim cb As CheckBox

			For Each dgi As DataGridItem In Me.dgMessagesSent.Items
				cb = DirectCast(dgi.Cells(0).Controls(1), CheckBox)
				cbCheckedValue = Request.Params(dgi.Cells(0).Controls(1).UniqueID)

				If cbCheckedValue <> Nothing AndAlso cbCheckedValue.Equals("on") Then
					Dim MessageID As Integer = Convert.ToInt32(cb.Attributes("value"))
					Dim mailMsg As Message = Messages.GetUserOutboxMessage(LoggedUser.ID, MessageID, False)
					mailMsg.IsMarkedDeleted = True
					mailMsg.Save()
				End If
			Next
			dgMessagesSent.DataBind()
			Response.Redirect(Constants.SENT_MESSAGE)
		End Sub

	End Class
End Namespace






